package br.ufrj.neg.reuniao.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.velocity.Template;
import org.apache.velocity.context.Context;
import org.apache.velocity.tools.view.VelocityViewServlet;

import br.ufrj.neg.bean.Idioma;
import br.ufrj.neg.constants.General;
import br.ufrj.neg.reuniao.bean.ArtefatoBean;
import br.ufrj.neg.reuniao.model.ArtefatoModel;
import br.ufrj.neg.reuniao.model.EncaminhamentoModel;
import br.ufrj.neg.reuniao.model.ParticipanteModel;
import br.ufrj.neg.reuniao.model.ReuniaoModel;
import br.ufrj.neg.servlet.PageContext;

public class CarregarResumoAction extends VelocityViewServlet{

	private static final long serialVersionUID = 3759364658257960936L;
	//private static Logger log = Logger.getLogger("rootLogger");
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		ReuniaoModel rm = new ReuniaoModel();
		ParticipanteModel pm = new ParticipanteModel();
		ArtefatoModel am = new ArtefatoModel();
		EncaminhamentoModel em = new EncaminhamentoModel();
		try {
			Idioma idioma = (Idioma) request.getAttribute("idioma");
		
			HttpSession session = request.getSession(false);
			synchronized(session){
				session.removeAttribute("NegociacaoBean");
				
			}
			Integer idReuniao = Integer.parseInt(request.getParameter("idReuniao"));
			setContentType(request, response);
			
			Context context = createContext(request, response);
			fillContext(context, request);
			
			ArtefatoBean ab = new ArtefatoBean(idioma);
			ab.setReuniao(idReuniao);
			
			context.put("pageContext", new PageContext(request));
			context.put("reuniao", rm.getReuniao(idReuniao));
			context.put("participantesDataSet", pm.listaParticipanteporReuniao(idReuniao));	
			context.put("artefatosDataSet", am.listaArtefatoporReuniao(ab));
			context.put("emcaminhamentoDataSet", em.listaEncaminhamentoReuniao(idReuniao));
			
			Template template = getTemplate(General.TEMPLATE_REUNIAO_PATH + "resumoReuniao.vm");
			mergeTemplate(template, context, response);
			
		} catch (Exception e) {
			System.out.println(e);
			request.getRequestDispatcher(General.TELA_ERRO_500).forward(request,
					response);
		}
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		doPost(request,response);
	}
}
